using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using System.Xml;
using Nop.Core.Domain.Payments;

namespace Nop.Plugin.Payments.WalletOne
{
	/// <summary>
	/// Represents WalletOne helper
	/// </summary>
	public class WalletOneHelper
	{
		public static string GetCurrencyNumericCode(string currencyCode)
		{
			string result = "643";

			if (currencyCode.ToUpper() != "RUB")
			{
				var currencies = new Dictionary<string, string>();

				var request = WebRequest.Create(String.Format("http://www.cbr.ru/scripts/XML_daily.asp?date_req={0:dd/MM/yyyy}", DateTime.Now)) as HttpWebRequest;
				using (WebResponse response = request.GetResponse())
				{
					var document = new XmlDocument();
					document.Load(response.GetResponseStream());

					var root = document.SelectSingleNode("ValCurs");

					foreach (XmlNode valute in root.ChildNodes)
					{
						currencies.Add(valute["CharCode"].InnerText, valute["NumCode"].InnerText);
					}
				}

				if (currencies.ContainsKey(currencyCode))
				{
					result = currencies[currencyCode];
				}
			}

			return result;
		}
	}
}